package com.toolbox.controller.infra;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.toolbox.annotation.OperateLog;
import com.toolbox.base.BaseController;
import com.toolbox.dto.InfraRoleDTO;
import com.toolbox.pages.Pager;
import com.toolbox.req.InfraRoleReq;
import com.toolbox.result.ResponseResultDTO;
import com.toolbox.service.InfraRoleService;
import com.toolbox.validation.group.CreateAction;
import com.toolbox.validation.group.UpdateAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Mango
 * @since 2023-12-01
 */
@SuppressWarnings("ALL")
@ApiSort(100)
@Api(tags = "角色表 前端控制器")
@RestController
@RequestMapping("/role-do")
public class InfraRoleController extends BaseController {

    @Autowired
    private InfraRoleService service;

    /**
     * 获取——角色列表（分页）
     *
     * @param pageIndex 当前页
     * @param pageSize  每页条数
     * @param keywords  搜索框（角色名称）
     * @return 集合
     */
    @ApiOperationSupport(order = 100)
    @ApiOperation(value = "获取——角色列表（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", example = "1", required = false, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", example = "10", required = false, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "keywords", value = "关键字", example = "", required = false, dataTypeClass = String.class)
    })
    @GetMapping(value = "/get-role-page")
    public ResponseResultDTO<Pager<InfraRoleDTO>> getRolePage(@RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              @RequestParam(name = "keywords", required = false) String keywords) {
        return ResponseResultDTO.success(service.getRolePage(pageIndex, pageSize, keywords));
    }

    /**
     * 获取——角色列表
     *
     * @return 集合
     */
    @ApiOperationSupport(order = 100)
    @ApiOperation(value = "获取——角色列表")
    @GetMapping(value = "/get-role-list")
    public ResponseResultDTO<List<InfraRoleDTO>> getRoleList() {
        return ResponseResultDTO.success(service.getRoleList());
    }

    /**
     * 创建——角色
     *
     * @param req 参数
     * @param bo  当前操作人
     * @return 主键ID
     */

    @OperateLog(operateName = "创建——角色", operateItem = "infra_role")
    @ApiOperationSupport(order = 100)
    @ApiOperation(value = "创建——角色")
    @PostMapping(value = "/create-role")
    public ResponseResultDTO<Long> createRole(@RequestBody @Validated(CreateAction.class) InfraRoleReq req) {
        return ResponseResultDTO.success(service.createRole(req, init()));
    }

    /**
     * 更新——角色
     *
     * @param req 参数
     * @param bo  当前操作人
     * @return 主键ID
     */
    @OperateLog(operateName = "更新——角色", operateItem = "infra_role")
    @ApiOperationSupport(order = 100)
    @ApiOperation(value = "更新——角色")
    @PutMapping(value = "/update-role-by-id")
    public ResponseResultDTO<Long> updateRoleById(@RequestBody @Validated(UpdateAction.class) InfraRoleReq req) {
        return ResponseResultDTO.success(service.updateRoleById(req, init()));
    }

    /**
     * 删除——角色
     *
     * @param id 主键ID
     */
    @OperateLog(operateName = "更新——角色", operateItem = "infra_role")
    @ApiOperationSupport(order = 100)
    @ApiOperation(value = "删除——角色")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class, paramType = "query", example = "1", required = true)
    @DeleteMapping(value = "/remove-by-id")
    public ResponseResultDTO<Void> removeById(@RequestParam(name = "id") Long id) {
        service.removeById(id);
        return ResponseResultDTO.success();
    }

}
